﻿using OOPA_opdracht.Model.Nodes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OOPA_opdracht.Model.FileFactory
{
    public class InputReader
    {
        private List<MainNode> theNodes;
        private string path;

        public InputReader(string filename = "input")
        {
            theNodes = new List<MainNode>();

            path = @"..\..\Model\FileFactory";
            path += @"\" + filename + ".txt";
        }

        public List<MainNode> read()
        {
            try
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    while (reader.Peek() != 0)
                    {
                        String readLine = reader.ReadLine();
                        //a # is commentary - don't add these lines
                        if (readLine != null)
                        {
                            if (!readLine.Contains('#'))
                            {
                                string[] lineParts = readLine.Split(':');
                                if (lineParts.Length > 1)
                                {
                                    lineParts[1] = lineParts[1].Trim();
                                    lineParts[1] = lineParts[1].Substring(0, lineParts[1].Length - 1);
                                    string[] soortOfOutputs = lineParts[1].Split(',');

                                    foreach (string soortOfOutput in soortOfOutputs)
                                    {
                                        MainNode node = MainNode.create(soortOfOutput);
                                        //if a new node is created, add it to our list of nodes
                                        if (node != null)
                                        {
                                            node.setName(lineParts[0]);
                                            node.setOutput();
                                            theNodes.Add(node);
                                        }
                                        else
                                        {
                                            //might be a node?! -- add neighbours to the nodes
                                            for (int i = 0; i < theNodes.Count; i++)
                                                if (theNodes[i].getName().Equals(soortOfOutput))
                                                    for (int j = 0; j < theNodes.Count; j++)
                                                        if (lineParts[0].Equals(theNodes[j].getName()))
                                                            theNodes[j].addNextNode(theNodes[i]);
                                        }
                                    }
                                }
                            }
                        } // end if (readLine != null)
                        else break;
                    } // end while peek() != 0
                } // end using streamreader

                List<MainNode> listOfInputs = new List<MainNode>();
                foreach (MainNode n in theNodes)
                    if(n.getOutput() != Inputs.NO_INPUT)
                        listOfInputs.Add(n);

                return listOfInputs;
            } // end try
            catch (Exception e)
            {
                throw new Exception("Failed: " + e.ToString());
            }      
        }
    }
}
